﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _70_7002_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            if (Request.QueryString["mode"] == "new")
            {
                this.DropDownList1.Items.FindByValue(this.Request.QueryString["sfuno"]).Selected = true;
                this.lab_title.Text = dao.Get_titleName(7002) + " - 新增";
                this.lab_cla.Text = "新增後不可更改";
            }else
            {
                this.lab_title.Text = dao.Get_titleName(7002) + " - 修改";
                this.hidd_no.Value = this.Request.QueryString["ID"];

                classification data = new classificationDAO().Get_classificaiton(int.Parse(this.hidd_no.Value));

                this.DropDownList1.Items.FindByValue(data.cla_sfuno.ToString()).Selected = true;
                this.DropDownList1.Enabled = false;
                this.tbox_name.Text = data.cla_name;
                this.lab_cla.Text = "不可更改";
            }
            
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.tbox_name.Focus();
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (checkInput())
        {
            classificationDAO dao = new classificationDAO();
            classification data;
            string str = "";

            //是：修改  否：新增
            if (this.hidd_no.Value != "")
            {
                data = dao.Get_classificaiton(int.Parse(this.hidd_no.Value));
                data.cla_sfuno = int.Parse(this.DropDownList1.SelectedItem.Value);
                data.cla_name = this.tbox_name.Text;
                str = "修改成功";
            }
            else
            {
                data = new classification();
                data.cla_sfuno = int.Parse(this.DropDownList1.SelectedItem.Value);
                data.cla_name = tbox_name.Text;
                data.state = "1";
                str = "新增成功";
            }
            data.createuid = new SessionObject().SessionUserID;
            data.createtime = DateTime.Now;

            //是：新增
            if (this.hidd_no.Value == "")
            {
                dao.Add_classification(data);
            }

            dao.Update();

            JsUtil.UpdateParentJs(this, str);

        }
    }

    private bool checkInput() 
    {
        if(string.IsNullOrEmpty(this.tbox_name.Text))
        {
            JsUtil.AlertJs(this, "請輸入分類名稱");
            return false;
        }

        return true;
    }
}